package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntNarrationMapper;
import com.prd.model.*;
import com.prd.service.*;
import com.prd.util.UserInfoUtil;
import com.prd.vo.EntNarrationRecordVo;
import com.prd.vo.EntNarrationVo;
import com.prd.vo.UserBaseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EntNarrationServiceImpl extends ServiceImpl<EntNarrationMapper, EntNarration> implements EntNarrationService {

    @Autowired
    private EntNarrationMapper entNarrationMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private EntNarrationCaseService entNarrationCaseService;

    @Autowired
    private EntNarrationInterviewService entNarrationInterviewService;

    @Autowired
    private EntNarrationRecordService entNarrationRecordService;

    @Autowired
    private EntNarrationSummaryService entNarrationSummaryService;

    @Override
    public int insertInfo(EntNarration entNarration) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entNarration);
        return entNarrationMapper.insert(entNarration);
    }

    @Override
    public int updateInfo(EntNarration entNarration) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entNarration);
        return entNarrationMapper.updateById(entNarration);
    }

    @Override
    public List<EntNarration> listInfo(String day, Integer catalog) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntNarration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(day), "DATE_FORMAT(start_time,'%m-%d') = {0}", day);
        //1：我发起
        if (catalog == 1) {
            queryWrapper.eq(EntNarration::getCreateId, loginUser.getId());
        } else if (catalog == 2) {//我接受
            queryWrapper.eq(EntNarration::getRecipient, loginUser.getId());
        } else if (catalog == 3) {//草稿
            queryWrapper.eq(EntNarration::getStatus, 0);
        } else if (catalog == 4) {//待面谈
            queryWrapper.eq(EntNarration::getStatus, 1);
        } else if (catalog == 5) {//已面谈
            queryWrapper.eq(EntNarration::getStatus, 2);
        } else if (catalog == 6) {//执行中
            queryWrapper.eq(EntNarration::getStatus, 3);
        } else {
            throw new RuntimeException("找不到类型！");
        }

        List<EntNarration> list = entNarrationMapper.selectList(queryWrapper);
        for (EntNarration narration : list) {
            List<Long> stringList = new ArrayList<>();
            stringList.add(StringUtils.isNotBlank(narration.getNarration()) ? 0 : Long.valueOf(narration.getNarration()));
            stringList.add(StringUtils.isNotBlank(narration.getRecipient()) ? 0 : Long.valueOf(narration.getRecipient()));
            List<UserBaseVo> list1 = userService.listName(stringList.toArray(new Long[stringList.size()]));
            for (UserBaseVo userBaseVo : list1) {
                if (narration.getNarration().equals(userBaseVo.getId().toString())) {
                    narration.setNarration(userBaseVo.getMerge());
                }
                if (narration.getRecipient().equals(userBaseVo.getId().toString())) {
                    narration.setRecipient(userBaseVo.getMerge());
                }
            }
        }
        return list;
    }

    @Override
    public int saveOrUpdateInfo(EntNarration entNarration, boolean b) {
        int i = 0;
        if (b) {
            i = insertInfo(entNarration);
        } else {
            i = updateInfo(entNarration);
        }
        List<EntNarrationCase> list = entNarration.getNarrationCase();
        list.stream().forEach(item -> {
            item.setNarrationId(entNarration.getId());
        });
        entNarrationCaseService.saveOrUpdateBatch(list);
        return i;
    }

    @Override
    public EntNarrationVo details(Long id) {
        EntNarrationVo entNarrationVo = new EntNarrationVo();
        EntNarration entNarration = entNarrationMapper.selectById(id);
        if (entNarration == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entNarration, entNarrationVo);

        List<Long> stringList = new ArrayList<>();
        stringList.add(StringUtils.isNotBlank(entNarration.getNarration()) ? 0 : Long.valueOf(entNarration.getNarration()));
        stringList.add(StringUtils.isNotBlank(entNarration.getRecipient()) ? 0 : Long.valueOf(entNarration.getRecipient()));
        List<UserBaseVo> list1 = userService.listName(stringList.toArray(new Long[stringList.size()]));
        for (UserBaseVo userBaseVo : list1) {
            if (entNarration.getNarration().equals(userBaseVo.getId().toString())) {
                entNarrationVo.setNarration(userBaseVo.getMerge());
            }
            if (entNarration.getRecipient().equals(userBaseVo.getId().toString())) {
                entNarrationVo.setRecipient(userBaseVo.getMerge());
            }
        }
        LambdaQueryWrapper<EntNarrationCase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EntNarrationCase::getNarrationId, id);
        List<EntNarrationCase> listCase = entNarrationCaseService.list(lambdaQueryWrapper);
        listCase.stream().forEach(item -> {
            if (item.getType() == 1) {
                entNarrationVo.getSummarize().add(item);
            }
            if (item.getType() == 2) {
                entNarrationVo.getAchievement().add(item);
            }
            if (item.getType() == 3) {
                entNarrationVo.getProblem().add(item);
            }
            if (item.getType() == 4) {
                entNarrationVo.getPlan().add(item);
            }
            if (item.getType() == 5) {
                entNarrationVo.getGuidance().add(item);
            }
        });
        LambdaQueryWrapper<EntNarrationInterview> interviewWrapper = new LambdaQueryWrapper<>();
        interviewWrapper.eq(EntNarrationInterview::getNarrationId, id);
        entNarrationVo.setInterview(entNarrationInterviewService.list(interviewWrapper));
        return entNarrationVo;
    }

    @Override
    public int submitReport(Long id) {
        EntNarration entNarration = entNarrationMapper.selectById(id);
        if (entNarration == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entNarration.setStatus(1);
        return updateInfo(entNarration);
    }

    @Override
    public EntNarrationRecordVo detailsRecord(Long interviewId) {
        EntNarrationRecordVo entNarrationRecordVo = new EntNarrationRecordVo();
        LambdaQueryWrapper<EntNarrationRecord> lambdaRecord = new LambdaQueryWrapper<>();
        lambdaRecord.eq(EntNarrationRecord::getInterviewId, interviewId);
        entNarrationRecordVo.setRecord(entNarrationRecordService.list(lambdaRecord));
        LambdaQueryWrapper<EntNarrationSummary> lambdaSummary = new LambdaQueryWrapper<>();
        lambdaSummary.eq(EntNarrationSummary::getInterviewId, interviewId);
        entNarrationRecordVo.setSummary(entNarrationSummaryService.list(lambdaSummary));
        return entNarrationRecordVo;
    }

    @Override
    public int addMake(Long id, Long[] makes) {
        EntNarration entNarration = entNarrationMapper.selectById(id);
        if (entNarration == null) {
            throw new RuntimeException("记录没有找到！");
        }
        if (StringUtils.isNotBlank(entNarration.getMake())) {
            entNarration.setMake(entNarration.getMake() + "," + makes.toString());
        } else {
            entNarration.setMake(makes.toString());
        }
        return updateInfo(entNarration);
    }

    @Override
    public int addGuidance(EntNarrationCase entNarrationCase) {
        return entNarrationCaseService.insertInfo(entNarrationCase);
    }
}
